package com.zsc.newbuds.config;
import java.util.LinkedHashMap;
import	java.util.Map;

import com.zsc.newbuds.component.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Shiro配置类
 * @author D
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){

        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);

        Map<String, String> filterMap = new LinkedHashMap<>();

        //静态资源
        filterMap.put("/asserts/css/**","anon");
        filterMap.put("/asserts/fonts/**","anon");
        filterMap.put("/asserts/images/**","anon");
        filterMap.put("/asserts/js/**","anon");
        filterMap.put("/asserts/json/**","anon");
        filterMap.put("/asserts/layui/**","anon");
        filterMap.put("/asserts/mods/**","anon");
        filterMap.put("/asserts/vendor/**","anon");


        filterMap.put("/main.html","authc");
        filterMap.put("/admin/**","authc");


        filterMap.put("/hello","perms[admin]");

        bean.setUnauthorizedUrl("/toLogin");

        bean.setLoginUrl("/toAdminLogin");


        bean.setFilterChainDefinitionMap(filterMap);

        return bean;

    }


    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }
}
